package com.gkd.controller.regular;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gkd.context.BaseContext;
import com.gkd.pojo.entity.Likes;
import com.gkd.result.Result;
import com.gkd.service.LikesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 点赞相关接口
 */
@RestController("regularLikesController")
@CrossOrigin
@Slf4j
@RequestMapping("/regular/likes")
public class LikesController {

    @Autowired
    private LikesService likesService;

    /**
     * 添加点赞
     * @param postId
     * @return
     */
    @PostMapping("/{postId}")
    public Result add(@PathVariable Integer postId){
        log.info("添加点赞:{}",postId);
        likesService.add(postId);
        return Result.success();
    }

    /**
     * 取消点赞
     * @param postId
     * @return
     */
    @DeleteMapping("/{postId}")
    public Result delete(@PathVariable Integer postId){
        log.info("取消点赞:{}",postId);
        likesService.delete(postId);
        return Result.success();
    }


    /**
     * 通过文章id和用户id获取点赞状态
     * @param postId
     * @return
     */
    @GetMapping("/{postId}")
    public Result getByPostId(@PathVariable Integer postId){
        Integer userId = BaseContext.getCurrentId();
        log.info("获取点赞状态:postId={},userId={}",postId,userId);
        String status=likesService.getByPostIdAndUserId(postId,userId);
        return Result.success(status);
    }

    /**
     * 通过文章id获取点赞总数
     * @param postId
     * @return
     */
    @GetMapping("/count/{postId}")
    public Result<Long> count(@PathVariable Integer postId){
        log.info("获取通过文章id获取文章收藏总数:{}",postId);
        LambdaQueryWrapper<Likes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Likes::getPostId,postId);
        Long count = likesService.count(wrapper);
        return Result.success(count);
    }
}
